package com.haighcom.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.haighcom.entity.PageResult;
import com.haighcom.mapper.CheckGroupMapper;
import com.haighcom.model.CheckGroup;
import com.haighcom.model.CheckItem;
import com.haighcom.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupMapper checkGroupMapper;

    /**
     * 添加检查组信息，通过map集合把检查组id和检查项id向后传递
     * @param checkGroup
     * @param checkItemIds
     */
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkItemIds) {
        checkGroupMapper.add(checkGroup);

        setCheckItemAndCheckGroup(checkGroup.getId(),checkItemIds);
    }

    @Override
    public PageResult queryPage(Integer currentPage, Integer pageSize, String queryString) {
        //开启分页
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckGroup> page = checkGroupMapper.selectAllByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public CheckGroup findById(Integer id) {
        CheckGroup checkGroup = checkGroupMapper.findById(id);
        return checkGroup;
    }

    /**
     * 通过检查组ID查找检查项ID
     * @param id
     * @return
     */
    @Override
    public List<Integer> findCheckItemsByCheckGroupId(Integer id) {

        return checkGroupMapper.findCheckItemsByCheckGroupId(id);
    }

    /**
     * 修改检查组信息
     * @param checkGroup
     * @param checkItemIds
     */
    @Transactional//这些事务要么全部完成，要么不完成回退
    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkItemIds) {
        int row = checkGroupMapper.deleteAllCheckGoupIds(checkGroup.getId());
        if(row != 0) {
            setCheckItemAndCheckGroup(checkGroup.getId(), checkItemIds);
        }
        checkGroupMapper.edit(checkGroup);
    }

    /**
     * 根据id删除检查组信息，删除之间检查检查组id是否被关联
     * 如果被关联就先把关联的检查项信息删除，然后把检查组信息删除
     * @param id
     */

    @Transactional
    @Override
    public void deleteById(Integer id) {
        List<Integer> checkItemIds = checkGroupMapper.findCheckItemsByCheckGroupId(id);
        if(checkItemIds != null){
            //关联表中有数据，删除关联表中的检查项和检查组信息
            checkGroupMapper.deleteAllCheckGoupIdsAndCheckItemIds(id);
        }
        checkGroupMapper.deleteCheckGroupById(id);
    }

    @Override
    public List<CheckGroup> findAll() {
        List<CheckGroup> checkGroups = checkGroupMapper.findAll();
        return checkGroups;
    }

    public void setCheckItemAndCheckGroup(Integer checkGroupId,Integer[] checkItemIds){
        if(checkItemIds != null && checkItemIds.length > 0){

            for(Integer checkItemId : checkItemIds) {
                HashMap<String,Integer> map = new HashMap();
                map.put("checkgroup_id", checkGroupId);
                map.put("checkitem_id", checkItemId);
                checkGroupMapper.setCheckItemAndCheckGroup(map);
            }

        }
    }
}
